package com.javasm.mingming.task.schedule;


import com.javasm.mingming.task.entity.SysTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @author javasm
 * @version 0.1
 * @className JavasmSchedulerConfigurer
 * @descriptioin:
 * @date 2025/1/17 10:05
 * @since jdk11
 */
@Component
public class JavasmSchedulerConfigurer implements SchedulingConfigurer {

    Logger logger = LogManager.getLogger(JavasmSchedulerConfigurer.class);

    private ScheduledTaskRegistrar scheduledTaskRegistrar;

    private Map<Integer,ScheduledTask> map = new ConcurrentHashMap<>();

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        //项目启动的时候,就已经调用了这个方法,是在启动成功之前调用的
        //考虑到有可能有多个定时任务,所以,创建一个线程池,专门用来存放定时任务
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
        ConcurrentTaskScheduler taskScheduler = new ConcurrentTaskScheduler(executorService);
        //开启注册 定时任务对象
        scheduledTaskRegistrar.setScheduler(taskScheduler);
        //放到全局变量,这样其他方法,就可以调用/使用  参数了
        this.scheduledTaskRegistrar = scheduledTaskRegistrar;
    }

    @Resource
    ApplicationContext applicationContext;
    public boolean regTask(SysTask task){
        if (task == null){
            return false;
        }
        //任务id
        Integer id = task.getId();
        //执行任务的类
        String clazz = task.getClazz();
        //表达式
        String cron = task.getCron();
        //new对象
        try {
            Class<?> aClass = Class.forName(clazz);
            //Object o = aClass.getConstructor().newInstance();
            Object bean = applicationContext.getBean(aClass);
            Runnable runnable = (Runnable) bean;
            //执行任务对象
            CronTask cronTask = new CronTask(runnable, cron);
            //任务开始,
            ScheduledTask scheduledTask = this.scheduledTaskRegistrar.scheduleCronTask(cronTask);
            //把已经开始的任务对象,存入全局,等待停止
            map.put(id,scheduledTask);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    public void stop(Integer id) {
        ScheduledTask scheduledTask = map.get(id);
        if (scheduledTask != null){
            //停止定时任务
            scheduledTask.cancel();
            map.remove(id);
        }
    }
}
